/**
 * Created by shq1 on 2017/2/21.
 */
import {getItem,setItem} from './LocalStorageUtil'
import {solveDate,toDecimal2} from './utils'
import {getHostUrl} from './Api'
//var Map = require('./Map');
//获取购物车数据
export function getCartStorage() {
  var carts = getItem("Cart")
  if (carts == null){
    carts = new Array();
  }
  return carts;
}
//重置购物车信息
export function setCart(carts) {
  setItem('Cart', carts);
}
//清空购物车数据
export function clearCartStorage() {
  setItem('Cart', "");
}
//计算购物车商品数量
export function countCartNum() {
  var CartList = getCartStorage();
  var num = 0;
  if (CartList != null && CartList.lenght != 0) {
    for (var i = 0; i < CartList.length; i++) {
      num += CartList[i].num;
    }
  }
  return num;
}
//获得购物车总价
export function getTotalPrice() {
  var CartList = getCartStorage();
  var num = 0;
  if (CartList != null && CartList.lenght != 0) {
    for (var i = 0; i < CartList.length; i++) {
      num += CartList[i].totalPrice;
    }
  }
  return num;
}

//加入购物车
export function addCartStorage(Cart, date, wday) {
  var CartList = getCartStorage();
  var curId = Cart.id;
  var flag = true;
  if (CartList == null || CartList.length == 0) {
    CartList = new Array();
  }
  for (var i = 0; i < CartList.length; i++) {
    var item = CartList[i];
    var id = item.productId;
    var itemDate = item.date;
    //购物车已有该商品，更新
    if (id == curId && date == itemDate) {
      flag = false;
      var num = parseInt(item.num) + 1;
      var totalPrice = parseFloat(item.price) * num;
      item.num = num;
      item.totalPrice = totalPrice;
      CartList[i] = item;
      break;
    }
  }
  //购物车没有该商品，增加
  if (flag) {
    var num = 1;
    var param = {
      image: solveImg(Cart.image),
      num: num,
      name: Cart.name,
      categoryId: Cart.categoryId,
      date: date,
      week: solveDate(wday),
      price: toDecimal2(Cart.price),
      totalPrice: Cart.price * num,
      productId: Cart.id,
      productClassId: Cart.productClassId
    };
    CartList.push(param);
  }
  setItem('Cart', CartList);
}
export function addCart(id,date) {
  var carts = getCartStorage();
  for (var i=0;i<carts.length;i++){
    if (id == carts[i].productId && date == carts[i].date){
      carts[i].num ++;
      carts[i].totalPrice = carts[i].price * carts[i].num;
      break;
    }
  }
  setCart(carts);
}
//删除购物车商品
export function delCart(id,date) {
  var carts = getCartStorage();
  for (var i=0;i<carts.length;i++){
    if (id == carts[i].productId && date == carts[i].date){
      if (carts[i].num > 1){
        carts[i].num --;
        carts[i].totalPrice = carts[i].price * carts[i].num;
      }else{
        carts.splice(i,1);
      }
      break;
    }
  }
  setCart(carts);
}
//修改时间
export function updateDate(id,oDate,nDate) {
  var carts = getCartStorage();
  for (var i=0;i<carts.length;i++){
    if (id == carts[i].productId && oDate == carts[i].date){
      var list = nDate.split("-");
      carts[i].date = list[0]+"-"+list[1]+"-"+list[2];
      carts[i].week = list[3];
      break;
    }
  }
  setCart(carts);
}
//图片处理
export function solveImg(img) {
  if(img.indexOf("http") < 0) {
    img = "http://" + getHostUrl() + img;
  }
  return img;
}

export function getOrderList() {
  var CartList = getCartStorage();
  var map = new Map();
  var vis = new Array(CartList.length);
  for (var i = 0; i < vis.length; i++) {
    vis[i] = false;
  }
  for (var i = 0; i < CartList.length; i++) {
    if (vis[i] == false) {
      var cur = CartList[i];
      var curCategoryId = cur.categoryId;
      var curProductId = cur.productId;
      var curDate = cur.date;
      var curMapList = new Array();
      curMapList.push(cur);
      for (var k = i + 1; k < CartList.length; k++) {
        if (vis[k] == false) {
          var item = CartList[k];
          if (item.productId == curProductId && curDate == item.date) {
            curMapList.push(item);
            vis[k] = true;
          }
        }
      }
       
      var mapList = solveMapData(curMapList);
      var mapKey = cur.productId + "," + cur.date;
      map.set(mapKey, mapList);
      vis[i] = true;
    }
  }
  var s = "{";
  for (var [key, value] of map) {
    s += "\"" + key +"\":";
    
    s += JSON.stringify(value);
    s += ",";
  }
  s = s.substring(0,s.length-1);
  s+="}";
  return JSON.parse(s);
}
export function solveMapData(list) {
  var newMapList = new Array();
  for (var i = 0; i < list.length; i++) {
    var item = list[i];
    var newItem = {
      "categoryId": item.categoryId,
      "date": item.date,
      "goodAccount": item.num,
      "goodId": item.productId,
      "goodImage": item.image,
      "goodName": item.name,
      "productClassId": item.productClassId
    };
    newMapList.push(newItem);
  }
  return newMapList;
}
